package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.CsOldAgePensionBean;
import com.wsc.wisdomchinaqinghe.view.MyToast;

/**
 * 进入法律援助申请表
 */
public class PensionPublishActivity extends BaseActivity implements View.OnClickListener{
    private ImageView regist_back;
    private TextView tv_headtitle;
    private PopupWindow popupWindow;
    private View mpopupWindowView;

    private TextView tv_cancel;
    private TextView tv_man;
    private TextView tv_woman;
    private MyToast myToast;
    private Context context;
    private String name;
    private EditText tv_yuyuename;
    private TextView tv_sex;
    private RelativeLayout layout_sex;
    private EditText et_shenfenzhenghao;
    private EditText et_phone;
    private Button bt_yuyue;
    private int type;
    private int subType;
    private String cid;
    private EditText et_country;//所在街道
    private EditText et_cun;//所在社区
    private EditText et_age;//年龄
    private EditText et_hujidizhi;//户籍地址
    private EditText et_juzhudizhi;//居住地址
    private EditText et_childname;//子女姓名
    private EditText et_zhixingbiaozhun;//执行标准
    private String source;
    private String previousId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pensionpublish_layout);
        context=this;
        myToast=new MyToast(context);
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        type=intent.getIntExtra("type",10);
        subType=intent.getIntExtra("subType",0);
        cid=intent.getStringExtra("cid");
        source=intent.getStringExtra("source");
        previousId=intent.getStringExtra("previousId");
        initView();
        initPopupWindow();
    }

    private void initView() {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText(name);
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
        tv_yuyuename=(EditText) findViewById(R.id.yuyue_name);
        tv_sex=(TextView) findViewById(R.id.tv_sex);
        layout_sex=(RelativeLayout) findViewById(R.id.layout_sex);
        layout_sex.setOnClickListener(this);
        et_shenfenzhenghao=(EditText) findViewById(R.id.tv_shenfenzhenghao);
        et_phone=(EditText) findViewById(R.id.et_phone);
        bt_yuyue=(Button) findViewById(R.id.bt_yuyue);
        bt_yuyue.setOnClickListener(this);

        et_country=(EditText) findViewById(R.id.et_country);//所在街道
        et_cun=(EditText) findViewById(R.id.et_cun);//所在社区
        et_age=(EditText) findViewById(R.id.et_age);//年龄
        et_hujidizhi=(EditText) findViewById(R.id.et_hujidizhi);//户籍地址
        et_juzhudizhi=(EditText) findViewById(R.id.et_juzhudizhi);//居住地址
        et_childname=(EditText) findViewById(R.id.et_childname);//子女姓名
        et_zhixingbiaozhun=(EditText) findViewById(R.id.et_zhixingbiaozhun);//执行标准
    }

    /**
     * 初始化popupwindow
     */
    private void initPopupWindow() {
        mpopupWindowView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.sexinfo, null);
        popupWindow = new PopupWindow(mpopupWindowView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);// 全屏显示，将内容设置在底部
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        tv_cancel = (TextView) mpopupWindowView.findViewById(R.id.tv_cancelsex);
        tv_cancel.setOnClickListener(this);
        tv_woman = (TextView) mpopupWindowView.findViewById(R.id.tv_takephotosex);
        tv_woman.setOnClickListener(this);
        tv_man = (TextView) mpopupWindowView.findViewById(R.id.tv_photosex);
        tv_man.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancelsex:
                popupWindow.dismiss();
                break;
            // woman
            case R.id.tv_takephotosex:
                popupWindow.dismiss();
                tv_sex.setText("女");
                break;
            // man
            case R.id.tv_photosex:
                popupWindow.dismiss();
                tv_sex.setText("男");
                break;
            case R.id.layout_sex:
                View view = getWindow().peekDecorView();
                if (view != null) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService
                            (Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }

                if (!popupWindow.isShowing()) {
                    popupWindow.showAtLocation(findViewById(R.id.layout_sex), Gravity.BOTTOM, 0, 0);// 在屏幕居中，无偏移
                } else {
                    popupWindow.dismiss();
                }
                break;
            case R.id.regist_back:
                finish();
                break;

            case R.id.bt_yuyue:
                String streetString=et_country.getText().toString().trim();
                String communityString=et_cun.getText().toString().trim();
                String nameString=tv_yuyuename.getText().toString().trim();
                String sexString=tv_sex.getText().toString().trim();
                String ageString=et_age.getText().toString().trim();
                String idString=et_shenfenzhenghao.getText().toString().trim();
                String houseHoldAddress=et_hujidizhi.getText().toString().trim();
                String liveAddress=et_juzhudizhi.getText().toString().trim();
                String childNameString=et_childname.getText().toString().trim();
                String operativeNormString=et_zhixingbiaozhun.getText().toString().trim();
                String phoneString=et_phone.getText().toString().trim();
                try {
                    postData(streetString,communityString,nameString,sexString,ageString,idString,houseHoldAddress,liveAddress,childNameString,operativeNormString,phoneString);
                }catch (Exception e)
                {

                }
                break;
        }
    }

    private void postData(String street, String community,String name,String sex,
                          String age,String idNumber,String houseHoldAddress,
                          String liveAddress,String childName,String operativeNorm,String phone)
    {
        if(street.isEmpty())
        {
            myToast.showShort("所在街道不能为空");
            return;
        }
        if(community.isEmpty())
        {
            myToast.showShort("所在社区不能为空");
            return;
        }
        if(name.isEmpty())
        {
            myToast.showShort("名字不能为空");
            return;
        }
        if(sex.isEmpty())
        {
            myToast.showShort("性别不能为空");
            return;
        }
        if(age.isEmpty())
        {
            myToast.showShort("年龄不能为空");
            return;
        }
        if(idNumber.isEmpty())
        {
            myToast.showShort("身份证号不能为空");
            return;
        }
        if(houseHoldAddress.isEmpty())
        {
            myToast.showShort("户籍地址不能为空");
            return;
        }
        if(liveAddress.isEmpty())
        {
            myToast.showShort("居住地址不能为空");
            return;
        }
        if(childName.isEmpty())
        {
            myToast.showShort("子女姓名不能为空");
            return;
        }
        if(operativeNorm.isEmpty())
        {
            myToast.showShort("执行标准不能为空");
            return;
        }
        if(phone.isEmpty())
        {
            myToast.showShort("联系电话不能为空");
            return;
        }
        if(!Tools.isMobile(phone))
        {
            myToast.showShort("电话号码格式错误");
            return;
        }

        if(idNumber.length()!=18)
        {
            myToast.showShort("身份证号格式错误");
            return;
        }

        CsOldAgePensionBean csOldAgePensionBean=new CsOldAgePensionBean();
        csOldAgePensionBean.setStreet(street);
        csOldAgePensionBean.setCommunity(community);
        csOldAgePensionBean.setName(name);
        csOldAgePensionBean.setSex(sex);
        csOldAgePensionBean.setAge(Integer.parseInt(age));
        csOldAgePensionBean.setIdNumber(idNumber);
        csOldAgePensionBean.setHouseHoldRegisterAddress(houseHoldAddress);
        csOldAgePensionBean.setLiveAddress(liveAddress);
        csOldAgePensionBean.setChildName(childName);
        csOldAgePensionBean.setOperativeNorm(operativeNorm);
        csOldAgePensionBean.setPhone(phone);

        String uid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
        Intent intent=new Intent(context,PensionPublishPhotoActivity.class);
        intent.putExtra("uid",uid);
        intent.putExtra("type",type);
        intent.putExtra("subType",subType);
        intent.putExtra("cid",cid);
        intent.putExtra("object",csOldAgePensionBean);
        intent.putExtra("previousId",previousId);
        startActivityForResult(intent,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0&&resultCode==1)
        {
            if(source!=null)
            {
                Intent intent=getIntent();
                setResult(1,intent);
                finish();
            }else {
                finish();
            }

        }

    }
}
